//
//  RootViewController.m
//  TestXcode5
//
//  Created by ileo on 13-11-13.
//  Copyright (c) 2013年 baozun. All rights reserved.
//

#import "BaseViewController.h"
#import "UIView+Extend.h"
#import "ImageHelper.h"
#import "ScreenMgr.h"

@interface BaseViewController ()

@property (nonatomic, assign) TYPE_MAIN_VIEW mainViewType;

@property (nonatomic, strong) UIScrollView *baseView;

@end

@implementation BaseViewController

- (instancetype)init
{
    self = [self initWithMainViewType:TYPE_MAIN_VIEW_NORMAL];
    if (self) {
        
    }
    return self;
}

-(id)initWithMainViewType:(TYPE_MAIN_VIEW)type{
    self = [super init];
    if (self) {
        self.mainViewType = type;
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

-(void)dealloc{
    if (self.baseView) {
        [self.baseView removeFromSuperview];
    }
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)viewDidLoad
{
    self.view.backgroundColor = [Theme backGroundColor];
    [super viewDidLoad];
    switch (self.mainViewType) {
        case TYPE_MAIN_VIEW_SCROLL_NORMAL:
        case TYPE_MAIN_VIEW_SCROLL_PULLDOWN:
        {
            UIScrollView *scrollV = [[UIScrollView alloc] initWithFrame:self.view.bounds];
            scrollV.contentSize = CGSizeMake(__MAIN_WIDTH, __MAIN_HEIGHT + 1);
            self.baseView = scrollV;
            [self.view addSubview:self.baseView];
        }
            break;

        case TYPE_MAIN_VIEW_TABLE_NORMAL:
        case TYPE_MAIN_VIEW_TABLE_PULLDOWN:
        {
            UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64)];
            tableView.backgroundColor = [UIColor clearColor];
            tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            self.baseView = tableView;
            [self.view addSubview:self.baseView];
        }
            break;
        default:
            break;
    }
    
    if (self.mainViewType == TYPE_MAIN_VIEW_TABLE_PULLDOWN ||
        self.mainViewType == TYPE_MAIN_VIEW_SCROLL_PULLDOWN){
        [self setScrollViewPullDown];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wakeFromBG) name:APP_WAKE_FROM_BG object:nil];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    SCREEN.currentViewController = self;
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -

-(void)wakeFromBG{
  
}

#pragma mark - refresh
-(UIScrollView *)scrollView{
    return self.baseView;
}

-(LoadMoreController *)loadMoreController{
    if (!_loadMoreController) {
        _loadMoreController = [[LoadMoreController alloc] initWithScrollView:self.baseView];
        _loadMoreController.delegate = self;
    }
    return _loadMoreController;
}

-(void)setScrollViewPullDown{
    LoadMoreView *topView = [[LoadMoreView alloc] initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, 45)];
    topView.tipsLoading = @"加载中";
    topView.tipsLoadingDone = @"加载完成";
    topView.tipsPulling = @"下拉即将加载";
    topView.tipsShouldLoad = @"松手立刻加载";
    self.loadMoreController.loadTopView = topView;
    self.loadMoreController.canAutoLoadTop = NO;
}

@end